.ui_silica_implies_sailfishos: &ui_silica_implies_sailfishos
  conditions:
    state: opened
    labels:
      - "UI::Silica"
    forbidden_labels:
      - "platform::SailfishOS"
  actions:
    labels:
      - "platform::SailfishOS"
.ui_kirigami_implies_plasma: &ui_kirigami_implies_plasma
  conditions:
    state: opened
    labels:
      - "UI::Kirigami"
    forbidden_labels:
      - "platform::Plasma Mobile"
  actions:
    labels:
      - "platform::Plasma Mobile"

.platform_sfos_in_title: &platform_sfos_in_title
  conditions:
    state: opened
    labels:
      - "platform::SailfishOS"
    ruby: not resource['title'].downcase.match(/^\[.*sailfish.*\]/)
  actions:
    comment: |
      /title [SailfishOS] #{resource['title']}
.platform_sfos_in_title: &sfos_in_title_implies_label
  conditions:
    state: opened
    forbidden_labels:
      - "platform::SailfishOS"
    ruby: resource['title'].downcase.match(/^\[.*sailfish.*\]/)
  actions:
    labels:
      - "platform::SailfishOS"

.platform_plasma_in_title: &platform_plasma_in_title
  conditions:
    state: opened
    labels:
      - "platform::Plasma Mobile"
    ruby: not resource['title'].downcase.match(/^\[.*plasma.*\]/)
  actions:
    comment: |
      /title [Plasma Mobile] #{resource['title']}
.platform_plasma_in_title: &plasma_in_title_implies_label
  conditions:
    state: opened
    forbidden_labels:
      - "platform::Plasma Mobile"
    ruby: resource['title'].downcase.match(/^\[.*plasma.*\]/)
  actions:
    labels:
      - "platform::Plasma Mobile"

.merge_two_tags: &merge_title_tags
  conditions:
    state: opened
    ruby: resource['title'].match(/^\[.*\]\s\[.*\]/)
  actions:
    comment: |
      /title [#{resource['title'].match(/^\[(.*)\]\s\[.*\]/)[1]}/#{resource['title'].match(/^\[.*\]\s\[(.*)\]/)[1]}] #{resource['title'].match(/^\[.*\]\s\[.*\]\s(.*)/)[1]}


resource_rules:
  epics:
    rules: []
    # Epics still seem quite buggy.
      # - name: Epic with UI::Silica gets platform::SailfishOS
      #   <<: *ui_silica_implies_sailfishos
      # - name: Epic with UI::Kirigami gets platform::Plasma Mobile
      #   <<: *ui_kirigami_implies_plasma
      #
      # - name: Epic with platform::SailfishOS gets [SailfishOS]
      #   <<: *platform_sfos_in_title
      # - name: Epic with platform::Plasma Mobile gets [Plasma Mobile]
      #   <<: *platform_plasma_in_title
      # - name: Epic with [SailfishOS] gets platform::SailfishOS
      #   <<: *sfos_in_title_implies_label
      # - name: Epic with [Plasma Mobile] gets platform::Plasma Mobile
      #   <<: *plasma_in_title_implies_label
      # - name: Merge multiple tags in epic title
      #   <<: *merge_title_tags
  issues:
    rules:
      - name: Issue with UI::Silica gets platform::SailfishOS
        <<: *ui_silica_implies_sailfishos
      - name: Issue with UI::Kirigami gets platform::Plasma Mobile
        <<: *ui_kirigami_implies_plasma

      - name: Issue with platform::SailfishOS gets [SailfishOS]
        <<: *platform_sfos_in_title
      - name: Issue with platform::Plasma Mobile gets [Plasma Mobile]
        <<: *platform_plasma_in_title
      - name: Issue with [SailfishOS] gets platform::SailfishOS
        <<: *sfos_in_title_implies_label
      - name: Issue with [Plasma Mobile] gets platform::Plasma Mobile
        <<: *plasma_in_title_implies_label
      - name: Merge multiple tags in issue title
        <<: *merge_title_tags

      - name: Unlabeled issues get ~"triage::needs attention"
        conditions:
          state: opened
          labels:
            - None
        limits:
          most_recent: 50
        actions:
          labels:
            - triage::needs attention
            - priority::unknown
          mention:
            - rubdos
          comment: |
            🤖 Bleep bloop, I'm a bot 🤖

            Hello {{author}} 👋, this issue is unlabelled 🏷️.
            If you cannot assign labels yourself, another Whisperfish developer will do this for you.
            Labeling this as ~"triage::needs attention" and ~"priority::unknown" for now.

      - name: Highly upvoted issues with a priority later than 2 get ~"priority::2"
        conditions:
          state: opened
          upvotes:
            attribute: upvotes
            condition: greater_than
            threshold: 2 ## change this when the community grows...
          ruby: "labels.none? { |l| l.name == 'priority::1' ||  l.name == 'priority::2' }"
        actions:
          labels:
            - priority::2
          mention:
            - rubdos
          comment: |
            🤖 Bleep bloop, I'm a bot 🤖

            📈 This issue is is quite popular, so we're putting the priority on ~"priority::2".

      - name: Very highly upvoted issues with a priority later than 2 get ~"priority::1"
        conditions:
          state: opened
          upvotes:
            attribute: upvotes
            condition: greater_than
            threshold: 4 ## change this when the community grows...
          ruby: "labels.none? { |l| l.name == 'priority::1' }"
        actions:
          labels:
            - priority::1
          mention:
            - rubdos
          comment: |
            📈 This issue is is ⭐️very popular⭐️, so we're putting the priority on ~"priority::1".
  merge_requests:
    rules:
      - name: Post CI build links
        conditions:
          state: opened
          ruby: "resource['description'].include?('/post_ci_links') && resource['source_project_id'] == resource['target_project_id']"
          forbidden_labels:
            - CI::has builds
            - CI::has no builds
        limits:
          most_recent: 50
        actions:
          labels:
            - CI::has builds
          comment: |
            🤖 Bleep bloop, I'm a bot 🤖
            I was instructed to provide links to CI builds, so here they are for
            [armv7hl](https://gitlab.com/#{project_path}/-/jobs/artifacts/#{resource['source_branch']}/browse/target/armv7-unknown-linux-gnueabihf/release/rpmbuild/RPMS/armv7hl/?job=build:armv7hl),
            [aarch64](https://gitlab.com/#{project_path}/-/jobs/artifacts/#{resource['source_branch']}/browse/target/aarch64-unknown-linux-gnu/release/rpmbuild/RPMS/aarch64/?job=build:aarch64),
            and
            [i486](https://gitlab.com/#{project_path}/-/jobs/artifacts/#{resource['source_branch']}/browse/target/i686-unknown-linux-gnu/release/rpmbuild/RPMS/i486/?job=build:i486)
            .
            Don't forget to revert to OpenRepos after your testing, because you will not receive updates until two new versions appear!

            These builds are only available and up-to-date when the ![pipeline status](https://gitlab.com/#{project_path}/badges/#{resource['source_branch']}/pipeline.svg) is green!
      - name: "Warn about CI build links when source != target"
        conditions:
          state: opened
          ruby: "resource['description'].include?('/post_ci_links') && resource['source_project_id'] != resource['target_project_id']"
          forbidden_labels:
            - CI::has builds
            - CI::has no builds
        limits:
          most_recent: 50
        actions:
          labels:
            - CI::has no builds
          comment: |
            🤖 Bleep bloop, I'm a bot 🤖
            I was instructed to provide links to CI builds, but I currenty do not have the ability to do this when the source project doesn't equal the target project. Panda is truly sorry! ☹️ 🐼
      - name: "Tag weblate MRs with ~translations"
        conditions:
          state: opened
          ruby: "resource['created_at'] > (Time.now - 24.hours).utc && author == 'weblate'"
          forbidden_labels:
            - translations
        actions:
          labels:
            - translations

      - name: "Merge requests with pending tasks but no pending threads"
        conditions:
          state: opened
          ruby: "resource['task_completion_status']['count'] > resource['task_completion_status']['completed_count']"
          forbidden_labels:
            - MR status::pending_tasks
        limits:
          most_recent: 50
        actions:
          labels:
            - MR status::pending tasks

      # In the category of "finished" MRs (no pending threads, no pending tasks), we still have draft status, merge_conflicts, and ready
      - name: "Draft merge requests without pending tasks and no pending threads"
        conditions:
          state: opened
          ruby: "resource['task_completion_status']['count'] == resource['task_completion_status']['completed_count'] && resource['work_in_progress']"
          forbidden_labels:
            - MR status::draft
        limits:
          most_recent: 50
        actions:
          labels:
            - MR status::draft
      - name: "Done merge requests without pending tasks and no pending threads with merge conflicts"
        conditions:
          state: opened
          ruby: "resource['task_completion_status']['count'] == resource['task_completion_status']['completed_count'] && ! resource['work_in_progress'] && ! resource['with_merge_status_recheck'] && resource['has_conflicts']"
          forbidden_labels:
            - MR status::merge conflict
        limits:
          most_recent: 50
        actions:
          labels:
            - MR status::merge conflict
      - name: "Done merge requests without pending tasks and no pending threads and no merge conflicts"
        conditions:
          state: opened
          ruby: "resource['task_completion_status']['count'] == resource['task_completion_status']['completed_count'] && ! resource['work_in_progress'] && ! resource['with_merge_status_recheck'] && ! resource['has_conflicts']"
          forbidden_labels:
            - MR status::ready
        limits:
          most_recent: 50
        actions:
          labels:
            - MR status::ready
      # TODO: it should be possible to do more intelligent things with `merge_status` (cannot_be_merged/can_be_merged/unchecked) and `with_merge_status_recheck`
